<script setup lang="ts">
import { ref } from 'vue'
import type { PostLoginParams } from '@/types/common'
import { getCodeAPI, loginMobileAPI } from '@/api/common'
import { useUserStore } from '@/store/user'

defineOptions({
  options: {
    styleIsolation: 'shared',
  },
})

// 倒计时
const showCountDown = ref(false)

// 获取验证码
const getCode = async () => {
  // 单独校验手机号
  try {
    // 如果手机号校验通过,返回(mobile:13*********),没通过直接走error
    if (await formRef.value?.validateField!('mobile')) {
      showCountDown.value = true
    }
    uni.showToast({
      title: '验证码发送成功',
      icon: 'success',
    })

    const res = await getCodeAPI(
      {
        mobile: form.value.mobile,
        type: 'login',
      },
      { custom: { isShowLoading: false } }
    )
    console.log(res)
    form.value.code = res.code
  } catch (error) {
    console.log('获取验证码错误', error)
  }
}

const form = ref<PostLoginParams>({
  mobile: '13230000032',
  code: '',
})

const isAgree = ref(false)

const rules = ref<UniHelper.UniFormsRules>({
  mobile: {
    rules: [
      { required: true, errorMessage: '请输入手机号' },
      { pattern: /^1[3-9]\d{9}$/, errorMessage: '请输入正确的手机号' },
    ],
  },
  code: {
    rules: [
      { required: true, errorMessage: '请输入验证码' },
      { pattern: /^\d{6}$/, errorMessage: '请输入正确的验证码' },
    ],
  },
})

const formRef = ref<UniHelper.UniFormsInstance>()

const handleSubmit = async () => {
  try {
    if (!isAgree.value) {
      uni.showToast({
        title: '请同意用户协议和隐私协议',
        icon: 'none',
      })
      return
    }
    await formRef.value?.validate?.()

    const res = await loginMobileAPI(form.value)
    // console.log(res)
    // 存储到pinia
    const userStore = useUserStore()
    userStore.setToken(res.token)
    userStore.setUserInfo(res)
    // 跳转到主页
    uni.showToast({ title: '登录成功!' })
    uni.switchTab({
      url: '/pages/index/index',
    })
  } catch (error) {
    console.log(error)
  }
}
</script>

<template>
  <uni-forms class="login-form" ref="formRef" :rules="rules" :model="form">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        v-model="form.mobile"
        :maxlength="11"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
        v-model="form.code"
        :maxlength="6"
      />
      <text class="text-button" v-if="!showCountDown" @click="getCode">获取验证码</text>
      <view class="text-button" v-else>
        <uni-countdown
          :show-day="false"
          :show-hour="false"
          :show-minute="false"
          :second="3"
          @timeup="showCountDown = false"
        />秒后重新获取
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" @click="isAgree = !isAgree" color="#16C2A3" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="handleSubmit">登 录</button>
  </uni-forms>
</template>

<style lang="scss">
@import './styles.scss';
</style>
